import { Form, Input, TreeSelect, DatePicker, Switch, Radio, InputNumber } from 'antd';
const FormBuilder = (data: BasicListApi.Field[] | undefined) => {
  return (data || []).map((item) => {
    const basicAttr = {
      label: item.title,
      name: item.key,
      key: item.key,
    };
    switch (item.type) {
      case 'text':
        return (
          <Form.Item {...basicAttr}>
            <Input disabled={item.disabled} />
          </Form.Item>
        );
      case 'tree':
        return (
          <Form.Item {...basicAttr}>
            <TreeSelect treeData={item.data} disabled={item.disabled} treeCheckable />
          </Form.Item>
        );
      case 'datetime':
        if (item.key !== 'update_time') {
          return (
            <Form.Item {...basicAttr}>
              <DatePicker disabled={item.disabled} showTime />
            </Form.Item>
          );
        }
        return null;
      case 'switch':
        return (
          <Form.Item {...basicAttr} valuePropName="checked">
            <Switch disabled={item.disabled} defaultChecked />
          </Form.Item>
        );
      case 'number':
        return (
          <Form.Item {...basicAttr}>
            <InputNumber disabled={item.disabled} />
          </Form.Item>
        );
      case 'textarea':
        return (
          <Form.Item {...basicAttr}>
            <Input.TextArea disabled={item.disabled} />
          </Form.Item>
        );
      case 'radio':
        return (
          <Form.Item {...basicAttr}>
            <Radio.Group buttonStyle="solid" defaultValue={item.data[0]?.value}>
              {(item.data || []).map((field: any) => {
                return <Radio.Button value={field.value}>{field.title}</Radio.Button>;
              })}
            </Radio.Group>
          </Form.Item>
        );
      case 'parent':
        return (
          <Form.Item {...basicAttr}>
            <TreeSelect
              showSearch
              style={{ width: '100%' }}
              dropdownStyle={{ maxHeight: 600, overflow: 'auto' }}
              treeData={item.data}
              placeholder="Please select"
              treeDefaultExpandAll
              allowClear
            />
          </Form.Item>
        );
      default:
        return null;
    }
  });
};
export default FormBuilder;
